package com.trsj.lnsj.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.trsj.lnsj.common.DataResponse;
import com.trsj.lnsj.entity.UserSignPersonDTO;
import com.trsj.lnsj.service.imp.UserSignPersonSerivce;
import com.trsj.lnsj.util.ResponseHelper;

/**
 * 
 * <p>
 * Title: UserSignPersonController.java
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2018
 * </p>
 * 
 * <p>
 * Company: 七色环
 * </p>
 * 
 * @author longky
 * 
 * @date 2018年9月15日
 * 
 * @version 1.0
 * 
 */
@RestController
@RequestMapping(value = "/api/usersignpersion/*")
@CrossOrigin
public class UserSignPersonController {
    @Autowired
    private UserSignPersonSerivce servie;

    @RequestMapping(value = "/list")
    public DataResponse userSignPersonList(@RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize, Long userId, HttpServletRequest request) {
        pageNum = pageNum - 1;
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);
        Page<UserSignPersonDTO> page = servie.selectByUserId(userId, pageRequest);
        return ResponseHelper.getSuccessResponse(page);
    }

    @RequestMapping(value = "/add")
    public DataResponse saveSignPerson(Long userId, String signPerson) {
        ArrayList<UserSignPersonDTO> persons = JSON.parseObject(signPerson,
                new TypeReference<ArrayList<UserSignPersonDTO>>() {
                });
        for (UserSignPersonDTO person : persons) {
            person.setId(userId);
        }
        List<UserSignPersonDTO> list = servie.save(persons);
        return ResponseHelper.getSuccessResponse(list);
    }
}
